package atom.core0.log;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Layout;
import org.apache.log4j.helpers.QuietWriter;
//解决log4j BufferedIO=true 时，buffer有残余日志不能记录的问题 

//使用异步输出 org.apache.log4j.AsyncAppender，异步输出必须使用xml方式配置才能支持，我把上面properties形式的配置文件用xml表达一下
public class DailyRollingFileAppender extends  
        org.apache.log4j.DailyRollingFileAppender {  
  
    public DailyRollingFileAppender() {  
        super();  
        Runtime.getRuntime().addShutdownHook(new Log4jHockThread());  
    }  
  
    public DailyRollingFileAppender(Layout layout, String filename,  
            String datePattern) throws IOException {  
        super(layout, filename, datePattern);  
        Runtime.getRuntime().addShutdownHook(new Log4jHockThread());  
    }  
  
    @Override  
    public synchronized void setFile(String fileName, boolean append,  
            boolean bufferedIO, int bufferSize) throws IOException {  
        File logfile = new File(fileName);  
  
        logfile.getParentFile().mkdirs();  
  
        super.setFile(fileName, append, bufferedIO, bufferSize);  
    }  
  
    public QuietWriter getQw() {  
        return super.qw;  
    }  
  
    private class Log4jHockThread extends Thread {  
        @Override  
        public void run() {  
            QuietWriter qw = DailyRollingFileAppender.this.getQw();  
            if (qw != null) {  
                qw.flush();  
            }  
        }  
    }  
}  
